package net.jiupinshi.daoProvider;

import net.jiupinshi.entity.ExpenseApply;

import java.text.MessageFormat;
import java.util.List;

/**
 * @Despription: 需要修改
 * @Author: penghui.guo
 * @Date: 2018/10/6 18:18
 */
public class ExpenseApplyProvider {

    public String insertExpenseApplys(List<ExpenseApply> expenseApplies){
        boolean flag = false;
        if(expenseApplies.get(0).getParentId() != null){
            flag = true;
        }
        StringBuilder sql = new StringBuilder(" insert into expense_apply(content, apply_user_name, expense_user_name," +
                "amount, apply_user_id, expense_user_id, number, expense_store_name, " +
                "expense_store_id, code");
        if(flag){
            sql.append(", parent_id");
        }
        sql.append(") values ");
        StringBuilder s = new StringBuilder("#{content}, #{applyUserName}, #{expenseUserName}, #{amount}" +
                ", #{applyUserId}, #{expenseUserId}, #{number}, #{expenseStoreName}" +
                ",#{expenseStoreId}, #{code}");
        if(flag){
            s.append(", #{parentId}");
        }
        System.out.println(s.toString());
        MessageFormat mf = new MessageFormat(s.toString());
        for (int i = 0; i < expenseApplies.size(); i++) {
            sql.append("(");
            sql.append(mf.format(new Object[]{i}));
            sql.append(")");
            if (i < expenseApplies.size() - 1) {
                sql.append(",");
            }
        }
        System.out.println(sql.toString());
        return sql.toString();
    }

    public String getExpenseApplyByStatusUid(Long applyUserId, Long operateUserId, Byte status, int start, int count){
        StringBuilder sql = new StringBuilder("select id, apply_user_name as applyUserName, expense_user_name as expenseUserName, apply_user_id as applyUserId " +
                ", expense_store_name as expenseStoreName, goods_name as goodsName, price, parent_id as parentId, goods_number as goodsNumber, code, DATE_FORMAT(create_time,'%Y-%m-%d %H:%i:%s') as createTime " +
                ", remark from expense_apply where ");
        getHisConditionSql(sql, applyUserId, operateUserId, status);
        sql.append(" order by id desc limit #{start},#{count}");
        return sql.toString();
    }

    public String getExpenseApplyCountByStatusUid(Long applyUserId, Long operateUserId, Byte status){
        StringBuilder sql = new StringBuilder("select count(id) from expense_apply where ");
        getHisConditionSql(sql, applyUserId, operateUserId, status);
        return sql.toString();
    }
    public void getHisConditionSql(StringBuilder sql, Long applyUserId, Long operateUserId, Byte status){
        if (applyUserId != null){
            sql.append(" apply_user_id = #{applyUserId}");
        }
        if (operateUserId != null){
            sql.append(" operate_user_id = #{operateUserId}");
        }
        if(status != null){
            sql.append(" and status = #{status}");
        }
    }
}
